'user strict'

const fs = require('fs')


class uploadStep{
    constructor () {
        this.param = '/'
      }
    
      async post (c) {

        console.log("调用step接口");

        let f = c.getFile('stepimg')

        let dirname = c.body.funcid;

        //解析文件扩展名
        let ext_name = c.helper.extName(f.filename)
      
        //生成唯一文件名
        let fname = `${c.helper.makeName()}${ext_name}`

        await fs.exists(`${c.service.publicPath}/usercreate/stepimage/${dirname}`,(exists)=>{
          //存在

          //不存在
          if(!exists){
            let dirpath = `${c.service.publicPath}/usercreate/stepimage/${dirname}`
            fs.mkdirSync(dirpath);
          }
        })
           
        let data = {
          funcid:c.body.funcid,
          stepid:c.body.stepid,
          dote1:c.body.dote1,
          dote2:c.body.dote2,
          text:c.body.steptext,
          imagename:fname
        }
    
        let data1 = await c.service.model.db_wxUserUpload.wxUserUploadStep(data);

        //将图片移入文件夹
        await c.moveFile(f, `${c.service.publicPath}/usercreate/stepimage/${dirname}/${fname}`)

        c.send(data1,200)
  
      }

}

module.exports = uploadStep